#define  _CRT_SECURE_NO_WARNINGS  1

#include"Stack.h"

void Stack::Init(int n)
{
	_array = (int*)malloc(sizeof(int)*n);
	if (_array == nullptr)
	{
		perror("malloc fail\n");
		exit(1);
	}
	_capacity = n;
	_top = 0;
}


void Stack::Push(int x)
{
	if (_capacity == _top)
	{
		int newCapacity = _capacity * 2;
		int* tmp = (int*)realloc(_array, sizeof(int) * newCapacity);
		if (realloc == nullptr)
		{
			perror("realloc fail\n");
			exit(1);
		}
			_array = tmp;
			_capacity = newCapacity;
	}
	_array[_top++] = x;
}
int Stack::Top()
{
	return _array[_top - 1];
}